using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class GameWidget : MonoBehaviour
{
	public static GameWidget currentWidget;

	public WidgetID type;

	public Text interactiveName;
	public Text[] interactiveInfo;
	public Image interactiveImage;

	public RectTransform arrowRight;
	public RectTransform arrowLeft;

	private bool leftOfObject;
	private Vector2 arrowPos;
	private Vector3 gameCamPos;
	private Vector3 widgetPos;
	private Transform interactive;
	private RectTransform trans;
	private Interactive interObj;

	public static void ShowWidget(Interactive _interactive, WidgetID _ID)
	{
		GameWidget obj = currentWidget;
		if (obj != null)
		{
			PoolManager.Instance.ReturnToPool(obj.gameObject);
		}

		obj = PoolManager.Instance.GetFromPool(_ID.ToString()).GetComponent<GameWidget>();
		ScreensManager.Instance.SetParent(obj.gameObject);
		obj.transform.SetAsFirstSibling();
		obj.Setup(_interactive);
	}

	private void Awake()
	{
		Character.characterModified += CharacterModified;
		trans = transform as RectTransform;
		
		arrowRight.gameObject.SetActive(false);
		arrowLeft.gameObject.SetActive(false);

		foreach (Text t in interactiveInfo)
		{
			t.gameObject.SetActive(false);
		}
	}

	private void Update()
	{
		if (interactive == null)
		{
			OnClick_Close();
			return;
		}

		UpdatePosition(interactive.position);
	}

	public void Setup(Interactive _interactive)
	{
		interObj = _interactive;
		currentWidget = this;
		interactive = _interactive.transform;
		SetupPosition(interactive.position);
		SetupInteractive(_interactive);
	}

	public void OnClick_Close()
	{
		currentWidget = null;
		PoolManager.Instance.ReturnToPool(gameObject);
	}

	private void CharacterModified()
	{
		if (interObj is Character)
		{
			SetupInteractive(interObj);
		}
	}

	private void SetupInteractive(Interactive _interactive)
	{
		interactiveName.text = _interactive.GetInfo().itemName;
		interactiveImage.sprite = _interactive.GetInfo().image;

		string[] strings = _interactive.GetInfo().GetStrings();
		for (int i = 0; i < strings.Length; i++)
		{
			interactiveInfo[i].text = strings[i];
			interactiveInfo[i].gameObject.SetActive(true);
		}
		for (int i = strings.Length; i < interactiveInfo.Length; i++)
		{
			interactiveInfo[i].gameObject.SetActive(false);
		}
	}

	private void SetupPosition(Vector3 _pos)
	{
		Vector3 gameCamPos = Camera.main.WorldToScreenPoint(_pos);
		if (gameCamPos.x >= (trans.sizeDelta.x + arrowRight.sizeDelta.x) * 1.25f)
		{
			leftOfObject = true;
		}
		else
		{
			leftOfObject = false;
		}
		
		arrowRight.gameObject.SetActive(!leftOfObject);
		arrowLeft.gameObject.SetActive(leftOfObject);
	}

	private void UpdatePosition(Vector3 _pos)
	{
		gameCamPos = Camera.main.WorldToScreenPoint(_pos);
		float camHeight = ScreensManager.Instance.cameraUI.pixelHeight;
		widgetPos = gameCamPos;

		if (leftOfObject)
		{
			widgetPos.x = widgetPos.x - (trans.sizeDelta.x * 0.55f + arrowRight.sizeDelta.x * 1f);
		}
		else
		{
			widgetPos.x = widgetPos.x + (trans.sizeDelta.x * 0.55f + arrowRight.sizeDelta.x * 1f);
		}

		widgetPos.y = gameCamPos.y + Mathf.Clamp01(gameCamPos.y / camHeight) * (trans.sizeDelta.y * 0.6f) - (trans.sizeDelta.y * 0.3f);
		widgetPos.x -= Screen.width / 2f;
		widgetPos.y -= Screen.height / 2f;
		trans.anchoredPosition = widgetPos;

		if (leftOfObject)
		{
			arrowPos = arrowLeft.anchoredPosition;
			arrowPos.y = gameCamPos.y - trans.anchoredPosition.y - Screen.height / 2f;
			arrowLeft.anchoredPosition = arrowPos;
		}
		else
		{
			arrowPos = arrowRight.anchoredPosition;
			arrowPos.y = gameCamPos.y - trans.anchoredPosition.y - Screen.height / 2f;
			arrowRight.anchoredPosition = arrowPos;
		}
	}
}
